function Player(){

	var that = this
	this.name;
	this.phase;
	this.numberofturns;
	this.alertPlayer;
	this.drawdeck;
	this.playhand;
	this.playarea;
	this.discarddeck;
	this.cardsstack;
	this.influence;
	this.haven;
	this.island;
	this.nativevillage;
	this.durationaffects;
	this.victorypoints;
	this.buycards;
	this.input;
	this.boughtcards;
	this.gainedcards;
	this.contrabandcards;
	this.horsetraders;
	this.reactions;
	this.screen;
	this.chat;
	this.gameoverdata;
	this.validundo;
	this.resources;
	this.isgameover;
	
	this.sec_screen = document.getElementById('sec_screen');	
	this.sec_choises = document.getElementById('sec_choises');	
	this.sec_buycards = document.getElementById('sec_buycards');
	this.sec_basic_info = document.getElementById('sec_basic_info');
	this.sec_board = document.getElementById('sec_player');
	this.sec_options = document.getElementById('sec_options');
	this.sec_chat = document.getElementById('sec_chat');
	this.sec_gameover = document.getElementById('sec_gameover');
	this.sec_endgame = document.getElementById('sec_endgame');
	 
	this.parseresults = function(data) {
		
		that.name = data.player.name;
		that.alertPlayer = data.alertPlayer;
		that.numberofturns = data.player.numberofturns
		that.phase = data.player.phase;
		that.drawdeck = data.player.drawDeck;
		that.playhand = data.player.playHand;
		that.playarea = data.player.playArea;
		that.discarddeck = data.player.discardDeck;
		that.cardsstack = data.player.cardsStack;
		that.influence = data.player.cardsInfluence;
		that.haven = data.player.haven;
		that.island = data.player.island;
		that.nativevillage = data.player.nativeVillage
		that.durationaffects = data.player.durationAffects;
		that.victorypoints = data.player.victorypoints;
		that.gainedcards = data.player.gainedcards;
		that.boughtcards = data.player.boughtcards;
		that.contrabandcards = data.player.contrabandcards;
		that.horsetraders = data.player.horsetraders;
		that.reactions = data.player.reactions;
		that.screen = data.screenmessages
		that.chat = data.chat
		that.gameoverdata = data.gameoverdata;
		that.validundo = data.player.validundo;
		that.resources = data.resources;
		that.isgameover = data.isGameOver;
		that.cointokens = data.player.cointokens;
		that.cointokensrequired = data.cointokensrequired;
		
		if (that.phase == 'Buy' || that.phase == 'Payment')
			that.buycards = data.buy.cards;
		else if (that.phase == "Input") 
			that.input = data.input;
        else if (that.phase == "BlackMarketPayment") {
            that.input = data.input;
        }
		else if (that.phase == "DigitalStorePayment") {
            that.input = data.input;
        }                        
			
		that.generatehtml()
	}
	
	this.clearInfo = function() {
		that.sec_basic_info.innerHTML = "";
		that.sec_board.innerHTML = "";
		that.sec_buycards.innerHTML = "";
		that.sec_choises.innerHTML = "";
		that.sec_options.innerHTML = "";
		that.sec_options.style.display = 'block';
		that.sec_screen.innerHTML = "";
	}
	
	
	this.generatehtml = function() {
		
		that.clearInfo();
		
		document.title = "Dominion";
		if (that.alertPlayer)
			AlertMe();
			
		that.generateScreen();
		that.generateChat();
		if (that.phase == "GameOver") {
			that.loadGameOverPage();
			that.generateEndGameData();
			return;
		}
		
		
		that.generateBasicInfo();
		that.generatePlayerDecksInfo();
		

		if (that.phase == "Action") {
			that.generateButtons(true, false, true, true, that.validundo);
		}
		else if (that.phase == "Payment") {
			that.generateButtons(false, true, true, true, that.validundo);
		}
		else if (that.phase == "Buy") {
			that.generateButtons(false, false, false, true, that.validundo)
		}
		else {
			that.generateButtons(false, false,  false, false, false)
		}
		
		if (that.phase == "Input") {
			that.generateInput();
		}
        else if (that.phase == "BlackMarketPayment") {
            that.generateInput();
        }
		else if (that.phase == "DigitalStorePayment") {
            that.generateInput();
        }
		else {
			clearInput();
		}	
	}
	
	this.loadGameOverPage = function () {
		
		var sec_gameover = document.getElementById('sec_gameover');	
		if (that.phase != "GameOver") {
			alert("game not over");
			return;
		}
		
		htmldata = ""
		otherdata = ""
		if (that.gameoverdata.gameAborted == true) {
			htmldata += "Game Aborted<br><br>";
			htmldata += that.gameoverdata.message;
			htmldata += "<br>";
		}
		else {
			if (that.gameoverdata.iWin == true) 
				htmldata += "Game over - You win!<br><br>";
			else 
				htmldata += "Game over - You lose<br><br>";
			var alen = that.gameoverdata.players.length;
			for ( var i=0, len=alen; i<len; ++i ){
				var player = that.gameoverdata.players[i];
				
				htmldata += player.name +"  -  VP: "+player.points + " ("+player.turns+" turns)<br>";	
				var cards_len = player.victory_cards.length;
				htmldata += "Victory Cards: " + player.victory_cards +"<br>";
				htmldata += "Victory Tokens: "+player.victory_tokens +"<br>";
			}
			htmldata += "<br><br><center><a href="+that.gameoverdata.logLink+" target='_blank' id='gameloglink'>game log</a></center><br>";
			htmldata += "<br>"
		}
		sec_gameover.innerHTML = htmldata;
		
		$("#sec_gameover").overlay({
			top: 260,
			mask: {
				color: '#fff',
				loadSpeed: 200,
				opacity: 0.5
				},
			closeOnClick: true,
			load: true
		});
		
		
	}
	
	this.generateEndGameData = function() {
		if (that.gameoverdata.gameAborted == true) {
			return
		}
		htmldata = ""
		var alen = that.gameoverdata.players.length;
		for ( var i=0, len=alen; i<len; ++i ){
			var player = that.gameoverdata.players[i];
			htmldata += displayBeautifulString(player.name, player.cards);
			htmldata += "<br>"
		}
		sec_endgame.innerHTML = htmldata;
	}
	
			
	this.generateChat = function() {
	
		var chatwindow = document.getElementById('chatwindow');
		var htmldata = ""
		
		var alen = that.chat.lines.length;
		var lastid = 0;
		for ( var i=0, len=alen; i<len; ++i ){
			htmldata += "<b>" + that.chat.lines[i].name +":</b> "+that.chat.lines[i].line+"<br>";
			lastid = that.chat.lines[i].id;
		}
		document.getElementById('last_chat_id').title = lastid;
		chatwindow.innerHTML = htmldata; 

		$('#line').autosize({append: "\n"})
		$('#chatwindow').prop("scrollTop", $("#chatwindow").prop('scrollHeight'));
		$('#chatfrm').keypress(function (e) {	
		  if (e.which == 13) {
			e.preventDefault();
			$('#chatfrm').submit();
		  }
		});
	}
	
	
	
	this.generateScreen = function() {
		
		var htmldata = "";
		htmldata += "<div id='logswindows' class='screen_messages'>";
		var alen = that.screen.lines.length;
		for ( var i=0, len=alen; i<len; ++i ){
			var cur_line = that.screen.lines[i];
			
			if (cur_line.params) {
				paramHtmldata = ""
				temp_line = cur_line.line;
				for (var j=0; j < cur_line.params.length; j++) {
					
					var cur_param = cur_line.params[j]
					if (cur_param.length < 2)
						continue;
					var name = cur_param['name'];
					var d_name = cur_param['d_name'];
					paramHtmldata = card2simpleHtmlWithParams(name, d_name)
					temp_line = temp_line.replace(/\[!\]/, paramHtmldata, 1);
				}
				
				temp_line = temp_line.replace(/=====/g, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
				htmldata += temp_line+"<br>"
			}
			else {
				htmldata += cur_line.line+"<br>";
			}
			
		}
		htmldata += "</div>";
		that.sec_screen.innerHTML = htmldata;
		that.sec_screen.scrollTop = that.sec_screen.scrollHeight;
		$('#logswindows').prop("scrollTop", $("#logswindows").prop('scrollHeight'));
	}
	
	this.generateBasicInfo = function() {
		var htmldata = ""
		//htmldata += "<b>Welcome "+that.name+",</b></br>";
		//htmldata += "Phase: "+that.phase+"</br>";
		//htmldata += "Turn Number: "+that.numberofturns+"</br>";
		that.sec_basic_info.innerHTML = htmldata;
		
	}
	
	this.generatePlayerDecksInfo = function() {
		var htmldata = "";

		if (that.island.length > 0)
			htmldata += cards2html('Island', that.island, null, null, null, true);
		if (that.durationaffects.length > 0)
			htmldata += cards2html('Duration Affects', that.durationaffects, null, null, null, true);
		if (that.nativevillage.length > 0)
			htmldata += cards2html('Native Village', that.nativevillage, null, null, null, true);
		if (that.haven.length > 0)
			htmldata += cards2html('Haven', that.haven, null, null, null, true);
			
		//htmldata += cardsNum2html('Draw Deck', that.drawdeck, null, true);
		//htmldata += cardsNum2html('Discard Deck', that.discarddeck, null, true);
		
		//htmldata += cards2html('Discard Deck', that.discarddeck, null, null, false);
		//htmldata += cards2html('Influence', that.influence, null, null, false);
		//htmldata += cards2html('Gained', that.gainedcards, null, false);
		//htmldata += cards2html('Bought', that.boughtcards, null, false);
		//htmldata += cards2html('Contraband', that.contrabandcards, null, false);
		//htmldata += cards2html('HorseTraders', that.horsetraders, null, false);
		//htmldata += cards2html('Reactions', that.reactions, null, false);
		
		//htmldata += cards2html('Play Hand', that.playhand, null, that.phase, "Hand");
		htmldata += cards2html('Play Hand', that.playhand, null, that.phase, "Hand", true);
		htmldata += cards2html('Play Area', that.playarea, null, null, null, true);
		that.sec_board.innerHTML = htmldata;
	}
	
	this.generateButtons = function(isBuy, isCoinTokens, isAllTr, isEndTurn, isUndo) {

		var htmldata = ""
		
		var enBuy = ""
		var enCoinTokens = ""
		var enAllTr = ""
		var enEndTurn = ""
		var enUndo = ""
		
		if (!isBuy)
			enBuy = "disabled"
		if (!isCoinTokens || this.cointokens < 1)
			enCoinTokens = "disabled"
		if (!isAllTr)
			enAllTr = "disabled"
		if (!isEndTurn)
			enEndTurn = "disabled"
		if (!isUndo)
			enUndo = "disabled"
		
		endTurnStyle = ""
		if (that.resources.buys == 0) {
			endTurnStyle = 'class = "end_turn_flash"';
		}
			
			
		htmldata += "<table><tr>";
		htmldata += "<td><form name='gotobuyfrm' action='javascript:goToBuy()'>                   <input "+enBuy+"        type='submit' name='SubmitForm' id='gotobuybtn' value='Go to Buy' />      </form></td>";
		if (this.cointokensrequired) {
			htmldata += "<td><form name='gotobuyfrm' action='javascript:playCoinTokens()'>            <input "+enCoinTokens+" type='submit' name='SubmitForm' id='gotobuybtn' value='Coin Tokens' />      </form></td>";
		}
		htmldata += "<td><form name='gotobuyfrm' action='javascript:goToBuyAndPlayAllTreasures()'><input "+enAllTr+"      type='submit' name='SubmitForm' id='playtreasuresbtn' value='Play Treasures' /> </form></td>";
		htmldata += "<td><form name='endturnfrm' action='javascript:endTurn()'>                   <input "+enEndTurn+"    type='submit' name='SubmitForm' id='endturnbtn' value='End Turn' "+endTurnStyle+"/>       </form></td>";
		htmldata += "<td><form name='endturnfrm' action='javascript:undo()'>                      <input "+enUndo+"       type='submit' name='SubmitForm' id='undobtn' value='Undo' />       </form></td>";
		
		htmldata += "</tr></table>";

				
		that.sec_choises.innerHTML = htmldata;
	}
		
	
	this.generateInput = function() {
           
		var htmldata = "";

		if (that.input.type == "ChooseOne" || that.input.type == "ChooseMany") {
			var className = that.input.type == "ChooseMany" ? "chooseMany" : "chooseOne";
			htmldata = "<table><tr><td>"+that.input.screen+"</td></tr><tr><td class='" + className + "'>";
			var alen = that.input.params.length;
			for ( var i=0, len=alen; i<len; ++i ){
			
				var option = that.input.params[i]
				if (allCardsArray[option])
					optionHtmldata = card2simpleHtmlWithParams(option, option);
				else
					optionHtmldata = option2simpleHtmlWithParams(option);
				
				if (that.input.type == "ChooseOne") {
					//htmldata += "<input type='radio' name='1' id='"+i+"' value='"+option+"'/>"
					//htmldata += "<span style='color:black;cursor:pointer;' onclick='playSingleInput("+i+")'>"+optionHtmldata+"</span>&nbsp;&nbsp;";
					htmldata += "<input type='radio' name='1' id='"+i+"' value='"+option+"'/>"+"<label for='" + i+ "'>" + optionHtmldata + "</label>";
				}
				else if (that.input.type == "ChooseMany") {
					htmldata += "<input type='checkbox' id='"+i+"' value='"+option+"'/>"+"<label for='" + i+ "'>" + optionHtmldata + "</label>";
				}
				else if (that.input.type == "Reorder") {
					htmldata += "<input type='input' id='"+i+"' value='"+option+"'/>"+optionHtmldata;
				}
			}
			
			htmldata += "</td><td><input type='button' onclick='playInput("+alen+")' value='Ok' />"
				
			htmldata += "</td></tr></table>"
			htmldata += "<input type='hidden' id='mincheckbox' value='"+that.input.min+"' />";
			htmldata += "<input type='hidden' id='maxcheckbox' value='"+that.input.max+"' />";
		}
		else if (that.input.type == "ChooseReorder") {
			
			htmldata = "<h3>" + that.input.screen + "</h3>";
	
			var alen = that.input.params.length;
			htmldata += "<ul id='sortable' style='list-style-type: none;padding:0;'>";
			for ( var i=0, len=alen; i<len; ++i ){
				htmldata += "<li style='cursor:move'><span>" + that.input.params[i]+"</span></li>";
			}
			htmldata += "</ul>";
			
			htmldata += "<form action='javascript:orderTableSubmit()'><input type='submit' name='SubmitForm' value='Ok' /></form>";
		}	
        that.sec_options.innerHTML = htmldata;
		if (that.input.type == "ChooseReorder") {
			jQuery("#sortable").sortable();
			$( "#sortable" ).disableSelection();
			$( "#sortable li span" ).mouseenter(function() { $(this).css('background-color', 'green');});
			$( "#sortable li span" ).mouseleave(function() { $(this).css('background-color', 'white');});
		}
	}
}